package com.spzx.product.controller;

import com.spzx.common.core.utils.ServletUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.Brand;
import com.spzx.product.service.BrandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author spzx
 * @since 2024-12-19
 */
@Slf4j
@Log4j2
@Tag(name = "品牌管理")
@RestController
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandService brandService;

    @Operation(summary = "获取全部品牌")
//    @GetMapping("/brand")
    @GetMapping("/getBrandAll")
    public AjaxResult getBrandAll() {
        return success(brandService.list());
    }

    /**
     * 查询品牌列表
     *
     * @param name
     * @return
     */
    //@RequiresPermissions : 权限注解，用于控制访问某个接口的权限
    //@Parameter : swagger注解，用于描述参数信息
    //description : 描述
    @Log(title = "品牌管理", businessType = BusinessType.OTHER)
    @RequiresPermissions("product:brand:list")
    @Operation(summary = "品牌列表")
    @GetMapping("/list")
    public TableDataInfo list(
            @Parameter(description = "品牌名称")
            String name) {

        startPage();
        List<Brand> list = brandService.selectBrandList(name);
        return getDataTable(list);
    }

    /**
     * 根据品牌编号获取详细信息
     *
     * @param id
     * @return
     */
    @Log(title = "品牌管理", businessType = BusinessType.OTHER)
    @RequiresPermissions("product:brand:query")
    @Operation(summary = "品牌详情")
    @GetMapping("/{id}")
    //@PathVariable : 路径变量注解，用于获取请求URL中的参数

    public AjaxResult getInfo(
            @Parameter(description = "品牌id" , example = "null")
            @PathVariable("id")
            Long id) {
        return success(brandService.getById(id));
    }

    /**
     * 品牌新增
     *
     * @param brand
     * @return
     */
    @Log(title = "品牌管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("product:brand:add")
    @Operation(summary = "品牌新增")
    @PostMapping()
    public AjaxResult add(
            @Parameter(description = "品牌", example = "新增的品牌信息")
            @Validated //解决前端传来的数据为空时，后端接收为null导致校验不通过的问题
            @RequestBody
            Brand brand) {
        if (!brandService.checkBrandNameUnique(brand)) {
            return error("新增品牌'" + brand.getName() + "'失败，品牌名称已存在");
        } else {
            brand.setCreateBy(SecurityUtils.getUsername());
            return toAjax(brandService.save(brand));
        }
    }

    /**
     * 品牌修改
     *
     * @param brand
     * @return
     */
    @Log(title = "品牌管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("product:brand:edit")
    @Operation(summary = "品牌修改")
    @PutMapping()
    public AjaxResult edit(
            @Parameter(description = "品牌" , example = "修改的品牌信息")
            @RequestBody
            Brand brand) {
        if (!brandService.checkBrandNameUnique(brand)) {
            return error("修改品牌'" + brand.getName() + "'失败，品牌名称已存在");
        }else {
            //设置当前用户
            brand.setUpdateBy(SecurityUtils.getUsername());
            return toAjax(brandService.updateById(brand));
        }

    }

    /**
     * 删除品牌
     *
     * @param ids
     * @return
     */

    @Log(title = "品牌管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("product:brand:remove")
    @Operation(summary = "品牌删除")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "品牌id列表", example = "[1,2]")
            @PathVariable List<Long> ids) {
        return toAjax(brandService.removeBatchByIds(ids));
    }

    @Log(title = "品牌管理", businessType = BusinessType.TEST)
    @Operation(summary = "测试")
    @GetMapping("/test")
    public AjaxResult test() {
        log.info("测试");
        return success();
    }
}
